package com.espirit.builder.base;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.espirit.eap.manager.EapImport;
import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.Layout;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.pagelayout.Page;
import com.espirit.eap.pagelayout.PageReference;
import com.espirit.eap.pagelayout.SectionElement;
import com.googlecode.cswish.struts.ContextHolder;
import com.googlecode.cswish.struts.Invoker;
import com.googlecode.cswish.struts.Routing;

@Component
public  class EapImportBuilder extends ModelBuilderSupport {
	protected static final String FIELD_FILE="file";
	protected static final String FIELD_REMARK="remark";
	
	
	@Override
	public void buildFields() {
		addModelField(FIELD_FILE, "File", "文件");
		addModelField(FIELD_REMARK, "Remark", "备注");
	}
	
	@Override
	protected void setDefaultLayoutButtons() {
		
	}
	
	@Override
	protected Integer getDetailLayoutSectionColsPerRow() {
		return 1;
	}
	
	@Override
	public void prepareLayoutElements() {
		ModelField mfFile = getModelField(FIELD_FILE);
		ModelField mfRemark = getModelField(FIELD_REMARK);

		addDetailField(mfFile, mfRemark);
		setRequired(FIELD_FILE);
		super.prepareLayoutElements();
	}

	@Override
	protected void afterBuilderLayout() {
		FlatSection fs = (FlatSection) model.getDefaultDetailLayout().getLastSection();
		model.getDefaultDetailLayout().findSectionElement(FIELD_REMARK).setReadOnly(true);
		
		PageReference eventReference = new PageReference();
		eventReference.setUrl(getReferenceUrl());
		elementService.createSectionElement(fs, eventReference);

		genericService.update(model);
		super.afterBuilderLayout();
	}
	
	protected String getReferenceUrl(){
		return "/pages/espirit/eap/manager/eapimport._importFile.ftl";
	}
	
	public Page buildImportPageLayout(String modelName, List<String> properties) {
		// create page link
		Page page = new Page();
		String simpleName = beanIntrospector.getSimpleName(modelName);
		String method = "analyseImportFileColumns";
		String path = '/' + Routing.invoker2PathNoExtension(simpleName,
				Invoker.DEFAULT_SEPARATOR, method, ContextHolder.get().getSiteName()) + ".html";
		page.setPath(path);
		page.setMethod(method);
		Page dbPage = genericService.searchAll(page).getData(0);
		if (dbPage != null) {
			page = dbPage;
		}
		
		if (dbPage == null || dbPage.getLayout() == null) {
			// create layout
			Layout layout = new Layout();
			layout.setCustomVersion(Boolean.FALSE);
			SectionElement element = new SectionElement();
			layout.safeChildren().add(element);
			element.setTagName("importsetting");
			String property = StringUtils.join(properties, "','");
			element.addExProperty("targetModelProperties", "['" + property + "']");
			layout = genericService.merge(layout);
			
			page.setLayout(layout);
		}
		
		page.setTemplate(Boolean.FALSE);
		genericService.merge(page);
		return page;
	}

	@Override
	public boolean init() {
		return super.setClass(EapImport.class, "Import", "批量导入");
	}
}
